﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicalManagementSystem.Utils;
using MedicalManagementSystem.Models;
using MedicalManagementSystem.Helpers;

namespace MedicalManagementSystem.Controllers
{
    public class DistrictController : GenericController
    {
        //
        // GET: /District/

        public ActionResult Index()
        {
            Menu.ID = "adm,district";
            foreach (var district in DbContext.Instance.District)
            {
                district.Load();
            }
            var districts = DbContext.Instance.District.OrderBy(district => district.Region.id).OrderBy(district => district.Name);
            return View(districts);
        }

        //
        // GET: /District/Details/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /District/Create
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /District/Create
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "id")]District district, int? RegionId)
        {
            district.Region = Region.GetByPK(RegionId != null ? RegionId.Value : 0);
            try
            {
                DbContext.Instance.AddToDistrict(district);
                DbContext.Instance.SaveChanges();

                return RedirectToAction("Index");
                
            }
            catch (RuleViolationException ruleException)
            {
                AddViolataionsToModelState(ruleException);
            }
            return View(district);
        }
        
        //
        // GET: /District/Edit/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        public ActionResult Edit(int id)
        {
            var district = District.GetByPK(id);
            district.Load();
            return View(district);
        }

        //
        // POST: /District/Edit/5
        [EnumAuthorize(Roles = UserRoles.RegistratorRole)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int? RegionId, District district)
        {
            try
            {
                var original = District.GetByPK(district.id);
                original.Region = RegionId != null ? Region.GetByPK(RegionId.Value) : null;

                DbContext.Instance.ApplyPropertyChanges(original.EntityKey.EntitySetName, district);
                DbContext.Instance.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (RuleViolationException ruleException)
            {
                AddViolataionsToModelState(ruleException);
            }

            return View(district);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Delete(int id)
        {
            try
            {
                var district = District.GetByPK(id);
                DbContext.Instance.DeleteObject(district);
                DbContext.Instance.SaveChanges(true);
            }
            catch (Exception e)
            {

            }

            return RedirectToAction("Index");
        }
    }
}
